package cn.itcast.jobs.service.impl;

import cn.itcast.jobs.dao.JobInfoDAO;
import cn.itcast.jobs.pojo.JobInfo;
import cn.itcast.jobs.service.JobInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class JobInfoServiceImpl implements JobInfoService{

    @Autowired
    private JobInfoDAO jobInfoDAO;
    @Override
    @Transactional
    public void save(JobInfo jobInfo) {
        //        查询信息，根据url和时间
        JobInfo param = new JobInfo();
        param.setUrl(jobInfo.getUrl());
        param.setTime(jobInfo.getTime());

        List<JobInfo> lis = this.findJobInfo(param);

//        判断数据库中是否有已存在的数据，
        if (lis.size() == 0) {

// 不存在执行新增// 如果已存在执行更新，
            this.jobInfoDAO.saveAndFlush(jobInfo);
        }
    }

    @Override
    public List<JobInfo> findJobInfo(JobInfo jobInfo) {

//        设置查询条件

        Example example = Example.of(jobInfo);
//执行查询
        List list = this.jobInfoDAO.findAll(example);
        return list;
    }

}
